
AddCSLuaFile( "cl_init.lua" )
AddCSLuaFile( "shared.lua" )
include( 'shared.lua' )

function ENT:SetPlayer(ply)
	self:SetOwner(ply)
end

function ENT:MakeActive()
	self.IsActive = true
	self.KillTime = CurTime() + 2
end

/*---------------------------------------------------------
   Name: Initialize
---------------------------------------------------------*/
function ENT:Initialize()

	self:SetModel( "models/props_junk/PropaneCanister001a.mdl" )
	self:PhysicsInit( SOLID_VPHYSICS )
	self:SetMoveType( MOVETYPE_VPHYSICS )
	self:SetSolid( SOLID_VPHYSICS ) 
	self:SetColor(225,200,200,255)
	self.EmitOnce = false
	self.HitCounter = math.random(2,5)
	self.Die = false
	self.IsActive = false
	
	local phys = self:GetPhysicsObject()
	if (phys:IsValid()) then
		phys:Wake()
		phys:EnableDrag(true)
		phys:EnableCollisions(true)
		phys:SetMass(100)
	end
	math.randomseed(CurTime())
	
end

function ENT:BombExplode()

	self:EmitSound(Sound("npc/attack_helicopter/aheli_mine_drop1.wav"),140,math.random(80,130))
	
	local effectdata = EffectData()
	effectdata:SetOrigin(self:GetPos())
	util.Effect("clust_explosion", effectdata)
	
	self:ClustletMaker(self:GetOwner(),self:GetPos(),3,460)  
	
	self:Fire("kill",1,0.001)

end

function ENT:ClustletMaker(owner,pos,num,launchpower)
	for i=1,num do
		local vec = Vector(math.Rand(launchpower*-1,launchpower),math.Rand(launchpower*-1,launchpower),launchpower/2)
		local cl = ents.Create( "bomb_clustlet" )
		cl:SetPos( pos + (VectorRand() * math.Rand(-5,5)) )
		cl:SetOwner( owner )
		cl:Spawn()
		cl:Activate()
		cl:SetPhysicsAttacker( self:GetOwner( ) )
		local phys = cl:GetPhysicsObject()
		if phys:IsValid() then
			phys:ApplyForceCenter(vec)
		end
	end
end

/*---------------------------------------------------------
   Name: Think
---------------------------------------------------------*/
function ENT:Think()
	
	if self.DelaySound then
		if self.DelaySound < CurTime() then
			self:EmitSound(self.PSound,100,self.DPitch)
			self.DelaySound = nil
			self:SetPhysicsAttacker( self:GetOwner( ) )
		end
	end
	if self.DelayParent then
		if self.DelayParent < CurTime() then
			self:SetParent(nil)
			self.DelayParent = nil
			self:MakeActive()
			local phys = self:GetPhysicsObject()
			if phys:IsValid() then
			phys:ApplyForceCenter(self:GetOwner():GetVehicle():GetVelocity())
			end
		end
	end

	if self.KillTime != nil then
		if self.KillTime < CurTime() then self:BombExplode() end
	end

	if self.IsActive == false then return end
	if self.Die then self:BombExplode() end
	
	if self:WaterLevel() > 0 then
		self.KillTime = CurTime() 
	end
  
 end 
 
 
 /*---------------------------------------------------------
   Name: takedamage
---------------------------------------------------------*/
 function ENT:OnTakeDamage( dmginfo )
	
	self.HitCounter = self.HitCounter - 1
	
	if self.HitCounter <= 0 then
		self.Die = true
	end
	
	// React physically when shot/getting blown
	self:TakePhysicsDamage( dmginfo )
	
end


/*---------------------------------------------------------
   Name: PhysicsCollide
---------------------------------------------------------*/
function ENT:PhysicsCollide( data, physobj )

	if (data.Speed > 400 && data.DeltaTime > 0.2 ) then
		self:BombExplode()
	elseif (data.Speed > 100 && data.DeltaTime > 0.2 ) then
		local s = "physics/metal/metal_box_impact_soft"..math.random(1,3)..".wav"
		util.PrecacheSound(s)
		self:EmitSound(s,math.random(60,100),math.random(80,120))
	end
end